#ifndef CM_DISPATCHER_H
#define CM_DISPATCHER_H
#include "Scheduler.h"
#include "PinName.h"
#include "DigitalOut.h"
#include "AnalogSample.h"
#include "DataStorage.h"

class CMDispatcher : public Scheduler
{
public:
    enum class CMChannelName{A0,A1,A2,A3};
    CMDispatcher(AnalogSample& as,DataStorage& ds,PinName pin_int);
    ~CMDispatcher();

    void begin();
    void schedule() override;
    void update() override;
    void cleanup()override;

    void set_sample_enabled(CMChannelName name,bool enable);
    void set_threshold_alarm_enabled(CMChannelName name,bool enable);

    void set_all_channel_sample_enabled(bool a0_enable,bool a1_enable,bool a2_enable,bool a3_enable);
    void set_all_channel_threshold_alarm_enabled(bool a0_enable,bool a1_enable,bool a2_enable,bool a3_enable);

    void clear_interrupt();

    data_storage_reg_read_t& get_reg_data(uint8_t reg_address);

protected:
    int apply_median_filter(uint8_t ch_id);
    bool apply_compare(uint8_t id);
private:
    AnalogSample& _as;
    DataStorage& _ds;
    DigitalOut _interrupt_pin;
    data_storage_reg_read_t _reg_read;
};
#endif